package com.cfx.exercise_38;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Random;

import org.jbarcode.JBarcode;
import org.jbarcode.encode.Code128Encoder;
import org.jbarcode.encode.InvalidAtributeException;
import org.jbarcode.paint.BaseLineTextPainter;
import org.jbarcode.paint.WidthCodedPainter;
import org.jbarcode.util.ImageUtil;

import com.lowagie.text.BadElementException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.CMYKColor;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

public class PDFClass {
	
	public static void main(String[] args) {
		Document doc = new Document();
		/*try {
			PdfWriter pw = PdfWriter.getInstance(doc, new FileOutputStream("HelloWorld.pdf"));
			doc.open();
			doc.add(new Paragraph("a hello world pdf document"));
			doc.addAuthor("zhangshuo");
			doc.addCreationDate();
			doc.addSubject("An example to show how attributes can be added to pdf files");
			doc.close();
			pw.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}*/
		
		//``````````````````````````````````````````````````````````````````````````````````````
		
		/*Random ran = new Random();
		try {
			PdfWriter pw = PdfWriter.getInstance(doc, new FileOutputStream("example"+ran.nextInt(100)+".pdf"));
			doc.open();
			
			 Paragraph text = new Paragraph("sopm colored paragraph text",FontFactory.getFont(FontFactory.HELVETICA, 8, Font.NORMAL, new CMYKColor(255, 0, 0, 0)));
		        doc.add(text);
		        
		        
			PdfPTable table = new PdfPTable(3);//3列
			table.setWidthPercentage(100);
			table.setSpacingBefore(10f);
			table.setSpacingAfter(50f);
			
			//设置行宽
			float[] colWidths = {0.5f,1f,1.5f};
			table.setWidths(colWidths);
			
			
			PdfPCell cell1 = new PdfPCell(new Paragraph("cell 1"));
				cell1.setBorderColor(Color.gray);
				cell1.setPaddingLeft(10);
	            cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
	            cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
            PdfPCell cell2 = new PdfPCell(new Paragraph("Cell 2"));
	            cell2.setBorderColor(Color.GREEN);
	            cell2.setPaddingLeft(10);
	            cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
	            cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
            PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 3"));
	            cell3.setBorderColor(Color.RED);
	            cell3.setPaddingLeft(10);
	            cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
	            cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);
	          
	        table.addCell(cell1);
	        table.addCell(cell2);
	        table.addCell(cell3);
	        
	        doc.add(table);
	        
	       
	        
	        doc.close();	        
	        pw.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		Random ran = new Random();
		//设置pagesize
		Rectangle rect = new Rectangle(PageSize.A4);
		rect.setBackgroundColor(Color.LIGHT_GRAY);
		doc.setPageSize(rect);
		doc.setMargins(20, 20, 45, 45);
		
		try {
			PdfWriter pw = PdfWriter.getInstance(doc, new FileOutputStream("pdf/example"+ran.nextInt(100)+".pdf"));
			//文档属性
			doc.addTitle("Title@sample"); // 标题 
			doc.addAuthor("Author@zhangsan");// 作者 
			doc.addSubject("Subject@iText sample");// 主题 
			doc.addKeywords("Keywords@iText");// 关键字 
			doc.addCreator("Creator@iText");// 创建者 
			//设置页头页尾
//			HeaderFooter hf = new HeaderFooter(new Paragraph(""), false);
//			hf.setAlignment(Element.ALIGN_CENTER);
//			HeaderFooter ft = new HeaderFooter(new Phrase(""), new Phrase(""));
//			ft.setAlignment(Element.ALIGN_CENTER);
//			doc.setHeader(hf);
//			doc.setFooter(ft);
			doc.open();
			Paragraph title = new Paragraph("this is a title");
			title.setAlignment(Element.ALIGN_CENTER);//设置对齐方式
			title.setLeading(1f);//设置行间距
			doc.add(title);
			Paragraph text = new Paragraph("This is a text file, which is used to test the use of the iText.");
			text.setFirstLineIndent(20f);
			text.setFont(new Font(10, 10f, 10, Color.RED));
			text.setSpacingBefore(50f);
			text.setSpacingBefore(50f);
			doc.add(text);
			
			JBarcode localJBarcode = new JBarcode(Code128Encoder.getInstance(),
					WidthCodedPainter.getInstance(), BaseLineTextPainter
							.getInstance());
			localJBarcode.setShowText(true);// 显示文本
			Image code128Image = null;
			try {
				BufferedImage localBufferedImage = localJBarcode.createBarcode("101102350648792");
				code128Image = Image.getInstance(ImageUtil.encode(
						localBufferedImage, "png"));
			} catch (BadElementException e) {
				e.printStackTrace();
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (InvalidAtributeException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			code128Image.setAlignment(Image.MIDDLE);// 浮动排版
			code128Image.setAbsolutePosition(300, 300);// 页面绝对定位坐标
			code128Image.scaleAbsolute(100, 40);// 设置宽高
			doc.add(code128Image);
			doc.close();
			pw.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
